package com.jk.util.autotest.util;

import sun.net.www.protocol.http.HttpURLConnection;

import java.io.*;
import java.net.URL;

/**
 * @author jk
 */
public class HttpUtil {

    public static String interfaceUtil(String path, String data){
        try {
            URL url = new URL(path);
            //打开和url之间的连接
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            PrintWriter out = null;
            conn.setRequestProperty("accept", "*/*");
            conn.setRequestProperty("connection", "Keep-Alive");
            conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)");
            conn.setConnectTimeout(3000);
            conn.setReadTimeout(3000);
            conn.setDoOutput(true);
            conn.setDoInput(true);
            if(data==null || "".equals(data)){
                conn.setRequestMethod("GET");
                conn.connect();
            }else {
                conn.setRequestMethod("POST");
                conn.connect();
                out = new PrintWriter(conn.getOutputStream());
                //获取URLConnection对象对应的输出流
                out.print(data);
                out.flush();
            }

            /**GET方法请求*****start*/
            /**
             * 如果只是发送GET方式请求，使用connet方法建立和远程资源之间的实际连接即可；
             * 如果发送POST方式的请求，需要获取URLConnection实例对应的输出流来发送请求参数。
             * */
            //conn.connect();
            /**GET方法请求*****end*/


            /***POST方法请求****start*/
            /*out = new PrintWriter(conn.getOutputStream());
            //获取URLConnection对象对应的输出流
            out.print(data);
            out.flush();*/
            //缓冲数据            */
            /***POST方法请求****end*/
            // /获取URLConnection对象对应的输入流
            InputStream is = conn.getInputStream();
            //构造一个字符流缓存
            BufferedReader br = new BufferedReader(new InputStreamReader(is));
            String str = "";
            while ((str = br.readLine()) != null) {
                str = new String(str.getBytes(), "UTF-8");
                //解决中文乱码问题
                return str;
            }
            is.close();
            conn.disconnect();
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        return "";
    }

    public static void main(String[] args) throws InterruptedException {
        String url = "http://183.201.238.21/siteepg/asset/queryByCatId?siteId=6106&catId=221820&provinceId=0&pageNo=1";
        Long start = System.currentTimeMillis();
        String total=null;
        Long time = null;
        while (true){
            String json = interfaceUtil(url,"");
            if(json==null || "".equals(json)){
                continue;
            }
            int indexStart = json.indexOf("\"total\":")+8;
            int indexStart2 = json.indexOf("\"tl\":")+5;
            int indexEnd = indexStart;
            int indexEnd2 = indexStart2;
            while (json.charAt(indexEnd)!=(int)','){
                indexEnd++;
            }
            while (json.charAt(indexEnd2)!=(int)'}'){
                indexEnd2++;
            }
            if(time==null){
                time = Long.valueOf(json.substring(indexStart2,indexEnd2));
            }
            if(total==null || "".equals(total)){
                total = json.substring(indexStart,indexEnd);
            }else if(!json.substring(indexStart,indexEnd).equals(total)){
                System.out.println(json.substring(indexStart,indexEnd));
                System.out.println("最后用时："+(Long.valueOf(json.substring(indexStart2,indexEnd2))-time));
                break;
            }
            System.out.println(total);
            System.out.println("用时"+((System.currentTimeMillis()-start)/1000)+"s");
            Thread.sleep(1000);
        }
    }

}
